export class Groups extends Phaser.GameObjects.Group {
  // 对象组
  constructor(scene) {
    super(scene)
  }

  // 获取一个成员 创建成员
  get(x, y, key, frame = null, option = {}) {
    let child;
    for (let ch of this.children.getArray()) {
      // 发现同key休眠成员 恢复他
      if (!ch.active && ch.texture.key == key) {
        child = ch
        child.setPosition(x, y)
        child.setActive(true)
        child.setVisible(true)
        break
      }
    }
    // 无则创建一个
    if (!child) {
      child = this.scene.matter.add.sprite(x, y, key, frame, option)
      child.setCollisionGroup(-1) // 取消碰撞组
      child.setFrictionAir(0) // 空气阻力为0
      child.setActive(true)
      child.setVisible(true)
      this.scene.add.existing(child)
      this.children.set(child)
    }
    return child
  }
  // 回收成员
  killAndHide(child) {
    super.killAndHide(child)
    if (this.children.contains(child)) {
      child.setPosition(-100, -100) // 传出屏幕外
      child.setVelocity(0, 0) // xy轴静止 原地罚站
    }
  }
  // 全部回收
  allkillAndHide(key) {
    this.children.iterate((child) => {
      if (key == undefined || key == child.texture.key) {
        this.killAndHide(child)
      }
    })
  }

}
